package com.lenovo.smarttraffic.ui.activity.stage6;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lenovo.smarttraffic.MainActivity;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.CarInfo;
import com.lenovo.smarttraffic.bean.UserBean;
import com.lenovo.smarttraffic.database.CarInfoBean;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.util.HttpUtils;
import com.lenovo.smarttraffic.util.Utils;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class UserCenterActivity extends BaseActivity {

    @Override
    protected int getLayout() {
        return R.layout.activity_user_info;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initToolBar(this, "个人中心");

        View avatar = findViewById(R.id.avatar);
        TextView name_tv = findViewById(R.id.name_tv);
        TextView sex_tv = findViewById(R.id.sex_tv);
        TextView phone_tv = findViewById(R.id.phone_tv);
        TextView id_tv = findViewById(R.id.id_tv);
        TextView time_tv = findViewById(R.id.time_tv);
        ListView car_lv = findViewById(R.id.car_lv);

        HttpUtils.post("get_all_user_info", new HttpUtils().setParam("UserName", com.lenovo.smarttraffic.MainActivity.userName),
                response -> HttpUtils.post("get_car_info", new HttpUtils().setParam("UserName", MainActivity.userName),
                        response2 -> HttpUtils.post("get_car_account_balance", new HttpUtils().setParam("CarId", 1).setParam("UserName", MainActivity.userName),
                                response3 -> {
                                    UserBean.ROWS_DETAIL user = new Gson().fromJson(response.toString(), UserBean.class).getROWS_DETAIL().get(1);
                                    CarInfo carInfo = new Gson().fromJson(response2.toString(), CarInfo.class);
                                    avatar.setBackgroundResource(user.getPsex().equals("男") ? R.mipmap.touxiang_2 : R.mipmap.touxiang_1);
                                    name_tv.setText("姓名：" + user.getPname());
                                    sex_tv.setText("性别：" + user.getPsex());
                                    phone_tv.setText("手机号码：" + user.getPtel());
                                    id_tv.setText("身份证号：" + user.getPcardid().substring(0, 6) + "*********" + user.getPcardid().substring(user.getPcardid().length() - 3, user.getPcardid().length()));
                                    time_tv.setText("注册时间：" + user.getPregisterdate().split(" ")[0]);

                                    List<CarInfoBean> carAllDBS = LitePal.findAll(CarInfoBean.class);
                                    if (carAllDBS.size() == 0) {
                                        for (int i = 0; i < carInfo.getROWS_DETAIL().size(); i++) {
                                            CarInfo.ROWSDETAILBean allcar = carInfo.getROWS_DETAIL().get(i);
                                            new CarInfoBean(allcar.getNumber(),
                                                    allcar.getCarnumber(), allcar.getPcardid(),
                                                    allcar.getBuydate(), allcar.getCarbrand()).save();
                                        }
                                    }
                                    List<String[]> strings = new ArrayList<>();
                                    //进行数据库读取
                                    for (int i = 0; i < carAllDBS.size(); i++) {
                                        if (carAllDBS.get(i).getPcardid().equals(carInfo.getROWS_DETAIL().get(1).getPcardid())) {
                                            String[] data = new String[3];
                                            data[0] = carAllDBS.get(i).getCarbrand();
                                            data[1] = carAllDBS.get(i).getCarnumber();
                                            data[2] = response3.getString("Balance");
                                            strings.add(data);
                                        }
                                    }

                                    car_lv.setAdapter(new UserCenterAdapter(strings));
                                })));
    }
}

class UserCenterAdapter extends BaseAdapter {

    private List<String[]> list;

    public UserCenterAdapter(List<String[]> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_user_cars, parent, false);
            holder.image = convertView.findViewById(R.id.image);
            holder.info_tv = convertView.findViewById(R.id.info_tv);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.image.setBackgroundResource(Utils.getCarBandRes(list.get(position)[0]));
        holder.info_tv.setText(list.get(position)[1] + "    余额：" + list.get(position)[2]);

        return convertView;
    }

    static class ViewHolder {
        View image;
        TextView info_tv;
    }
}
